var express = require("express");
var router = express.Router();
const { userModel, doctorModel } = require("../module/module");

// 获取用户信息
router.get("/getUserMessage", async (req, res) => {
  try {
    const msg = await userModel.find();
    console.log(msg);
    res.json(msg);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

// 获取医生信息
router.get("/getDoctorMessage", async (req, res) => {
  try {
    const msg = await doctorModel.find();
    console.log(msg);
    res.json(msg);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

// 添加信息
router.post("/addUserMessage", async (req, res) => {
  try {
    const { _id, message, time } = req.body;
    const user = await userModel.findOne({ _id: _id });
    
    
    if (!user) {
      return res.status(404).json({ message: "用户不存在" });
    }
    user.detailed.push({ message, time });
    await user.save();
    res.status(201).json({ message: "添加成功", success: true });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

router.post("/addDoctorMessage", async (req, res) => {
  try {
    const { _id, message, time } = req.body;
    const doctor = await doctorModel.findOne({ _id: _id });
    if (!doctor) {
      return res.status(404).json({ message: "医生不存在" });
    }
    doctor.detailed.push({ message, time });
    await doctor.save();
    res.status(201).json({ message: "添加成功", success: true });
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
});

module.exports = router;
